package com.wingsboat.data.transmission.controller;

import cn.hutool.core.io.FileUtil;
import com.wingsboat.data.transmission.common.constant.ConsumerTopicConstant;
import com.wingsboat.data.transmission.common.domain.AjaxResult;
import com.wingsboat.data.transmission.config.WingsBoatConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * @Author: wangzhong
 * @Date: 2023/11/7 14:43
 * @Description:
 **/
@RestController
@RequestMapping("/kafkaMessage")
public class KafkaController {
    @Autowired
    KafkaTemplate<String, String> wingsboatKafkaTemplate;


    @Transactional
    @PostMapping("/send")
    public AjaxResult kafkaMessageSend(@RequestBody Map<String, String> params){
        wingsboatKafkaTemplate.send(new ProducerRecord<>(String.valueOf(params.get("topic")),String.valueOf(params.get("msg"))));
        return AjaxResult.success("success-"+params.get("topic"));
    }

    @Transactional
    @GetMapping(value = "/testGetDefectStatistics")
    public AjaxResult testGetDefectStatistics() throws Exception
    {
        String filePath = WingsBoatConfig.getResourcePath() + "缺陷月份-季度分布统计.txt";
        String str = FileUtil.readUtf8String(filePath);
        ProducerRecord<String, String> record = new ProducerRecord<>(ConsumerTopicConstant.WINGSBOAT_MSG_GT_DEVICE, "GZ", str);
        wingsboatKafkaTemplate.send(record);
        return AjaxResult.success();
    }

    @Transactional
    @GetMapping(value = "/testGetJcwDevice")
    public AjaxResult testGetJcwDevice()
    {
        String filePath = WingsBoatConfig.getResourcePath() + "device.txt";
        String str = FileUtil.readUtf8String(filePath);
        ProducerRecord<String, String> record = new ProducerRecord<>(ConsumerTopicConstant.WINGSBOAT_MSG_GT_REPAIR, "GZ", str);
        wingsboatKafkaTemplate.send(record);
        return AjaxResult.success();
    }

    @Transactional
    @GetMapping(value = "/testGetJcwRepair")
    public AjaxResult testGetJcwRepair()
    {
        //文件地址
        String filePath = WingsBoatConfig.getResourcePath() + "repair.txt";
        String str = FileUtil.readUtf8String(filePath);
        ProducerRecord<String, String> record = new ProducerRecord<>(ConsumerTopicConstant.WINGSBOAT_MSG_GT_STATISTIC, "GZ", str);
        wingsboatKafkaTemplate.send(record);
        return AjaxResult.success();
    }

    @Transactional
    @GetMapping(value = "/testGetDefectStatisticsCheck")
    public AjaxResult testGetDefectStatisticsCheck() throws Exception
    {
        String filePath = WingsBoatConfig.getResourcePath() + "缺陷月份-季度分布统计对账.txt";
        String str = FileUtil.readUtf8String(filePath);
        ProducerRecord<String, String> record = new ProducerRecord<>(ConsumerTopicConstant.WINGSBOAT_MSG_GT_DEVICE, "GZ", str);
        wingsboatKafkaTemplate.send(record);
        return AjaxResult.success();
    }

    @Transactional
    @GetMapping(value = "/testGetJcwDeviceCheck")
    public AjaxResult testGetJcwDeviceCheck()
    {
        String filePath = WingsBoatConfig.getResourcePath() + "deviceCheck.txt";
        String str = FileUtil.readUtf8String(filePath);
        ProducerRecord<String, String> record = new ProducerRecord<>(ConsumerTopicConstant.WINGSBOAT_MSG_GT_REPAIR, "GZ", str);
        wingsboatKafkaTemplate.send(record);
        return AjaxResult.success();
    }

    @Transactional
    @GetMapping(value = "/testGetJcwRepairCheck")
    public AjaxResult testGetJcwRepairCheck()
    {
        //文件地址1
        String filePath = WingsBoatConfig.getResourcePath() + "repairCheck.txt";
        String str = FileUtil.readUtf8String(filePath);
        ProducerRecord<String, String> record = new ProducerRecord<>(ConsumerTopicConstant.WINGSBOAT_MSG_GT_STATISTIC_CHECK, "GZ", str);
        wingsboatKafkaTemplate.send(record);
        return AjaxResult.success();
    }
}
